#_*_ encoding:utf-8 _*_

import os
import xml.dom.minidom
import codecs

#
# 配置文件位置
#

ddpos_config_path = "C:\Program Files (x86)\Meiji\Client\DDPOS.exe.config"
ddpos_serv_config_path = "C:\Program Files (x86)\Meiji\Service\DDPosSvr.exe.config"


## XP
xp_ddpos_config_path = "C:\Program Files\Meiji\Client\DDPOS.exe.config"
xp_ddpos_serv_config_path = "C:\Program Files\Meiji\Service\DDPosSvr.exe.config"

#
# 中间文件
#
ddpos_config_temp_path = "C:\Program Files (x86)\Meiji\Client\ddpos.temp"
ddpos_serv_config_temp_path = "C:\Program Files (x86)\Meiji\Service\ddpos.temp"

#
# XP中间文件
#
xp_ddpos_config_temp_path = "C:\Program Files (x86)\Meiji\Client\ddpos.temp"
xp_ddpos_serv_config_temp_path = "C:\Program Files (x86)\Meiji\Service\ddpos.temp"


#
# 判断文件是否存在
#

def is_file_exist(filepath):
	return os.path.isfile(filepath)

#
# 清理中间文件
#
def clear_temp_files():
	if is_file_exist(ddpos_config_temp_path):
		os.remove(ddpos_config_temp_path)
	if is_file_exist(ddpos_serv_config_temp_path):
		os.remove(ddpos_serv_config_temp_path)
#
# 客户端文件的修改
#

def modify_ddpos_config(filepath):

	print("修改DDPOS客户端配置文件\n")

	dom = xml.dom.minidom.parse(filepath)
	root = dom.documentElement
	
	addKeys = root.getElementsByTagName("add")
	
	for i in range(0, len(addKeys)):
		key = addKeys[i]
		
		if key.getAttribute('key') == 'ShowMember':
			member = input("是否显示会员(0: 否，1:是):")
			
			shop_id = ""
			if member == '1':
				shop_id = input("输入商家的ID:")
				print(shop_id)
			else:
				key.setAttribute('value', '0')
			
			for j in range(0, len(addKeys)):
				key = addKeys[j]
				if key.getAttribute('key') == 'Shop_id':
					key.setAttribute('value', shop_id)


					with codecs.open(ddpos_config_temp_path, 'w', encoding='utf-8') as f:
						dom.writexml(f, addindent='', newl='', encoding='utf-8')
						f.close()

						os.remove(ddpos_config_path)
						os.rename(ddpos_config_temp_path, ddpos_config_path)

						print("修改成功!\n")


#
# 服务端文件的修改
#
def modify_ddpos_serv_config(filepath):
	print("修改DDPOS服务端配置文件\n")

	dom = xml.dom.minidom.parse(filepath)
	root = dom.documentElement

	addKeys = root.getElementsByTagName("add")

	for i in range(0, len(addKeys)):
		key = addKeys[i]

		if key.getAttribute('key') == 'shop_id':
			shop_id = input("输入商家的ID:")
			key.setAttribute('value', shop_id)

	for i in range(0, len(addKeys)):
		key = addKeys[i]

		if key.getAttribute('key') == 'shop_key':
			shop_key = input("输入shop_key:")
			key.setAttribute('value', shop_key)

	for i in range(0, len(addKeys)):
		key = addKeys[i]

		if key.getAttribute('key') == 'mainUrl':
			mainurl = input("输入mainUrl(默认: user.diandianwaimai.com):")
			if len(mainurl) == 0:
				mainurl = "user.diandianwaimai.com"
			key.setAttribute('value', mainurl)

	for i in range(0, len(addKeys)):
		key = addKeys[i]

		if key.getAttribute('key') == 'CloudUrl':
			cloudUrl = input("输入ClodUrl(默认: pos.diandian-tech.com):")
			if len(cloudUrl) == 0:
				cloudUrl = "pos.diandian-tech.com"
			key.setAttribute('value', cloudUrl)

	with codecs.open(ddpos_serv_config_temp_path, 'w', encoding="utf-8") as f:
		dom.writexml(f, addindent='', newl='', encoding='utf-8')
		f.close()

		os.remove(ddpos_serv_config_path)
		os.rename(ddpos_serv_config_temp_path, ddpos_serv_config_path)

		input("修改成功！ 按任意键退出!")

#
# 检测DDPOS客户端配置文件是否存在
#
def is_ddpos_config_existed(win7_path, xp_path):

	if (is_file_exist(win7_path) == False) and (is_file_exist(xp_path) == False):
		return False

	if is_file_exist(xp_path):
		ddpos_config_path = xp_path
		ddpos_config_temp_path = xp_ddpos_config_temp_path

	return True

#
# 检测DDPOS服务端配置文件是否存在
#
def is_ddpos_serv_config_existed(win7_path, xp_path):

	if (is_file_exist(win7_path) == False) and (is_file_exist(xp_path) == False):
		return False

	if is_file_exist(xp_ddpos_serv_config_path):
		ddpos_serv_config_path = xp_path
		ddpos_serv_config_temp_path = xp_ddpos_serv_config_temp_path

	return True



if __name__ == '__main__':
	
	print("\n ======================= \n")
	print(" DDPOS配置文件修改")
	print("\n ======================= \n")
	
	if is_ddpos_config_existed(ddpos_config_path, xp_ddpos_config_path) == False:
		print(" DDPOS客户端配置文件不存在，请检查是否安装DDPOS！")
	
	if is_ddpos_serv_config_existed(ddpos_serv_config_path, xp_ddpos_serv_config_path) == False:
		print(" DDPOS服务端配置文件不存在，请检查是否安装DDPOS！")

	# 清理中间文件
	clear_temp_files()
		
	# 修改客户度配置文件
	modify_ddpos_config(ddpos_config_path)
	
	# 修改服务端配置文件
	modify_ddpos_serv_config(ddpos_serv_config_path)
		
	
		
	